package com.jw.server.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.ydx.protobuf.OTIpcDef;
import cn.ydx.protobuf.OTIpcDef.OTIpcList;

import com.jw.server.model.BaseInfoCompany;
import com.jw.server.model.BaseInfoCompanyFare;
import com.jw.server.model.BaseInfoCompanyPay;
import com.jw.server.model.BaseInfoCompanyPermit;
import com.jw.server.model.BaseInfoCompanyService;
import com.jw.server.model.BaseInfoCompanyStat;
import com.jw.server.model.BaseInfoDriver;
import com.jw.server.model.BaseInfoDriverApp;
import com.jw.server.model.BaseInfoDriverEducate;
import com.jw.server.model.BaseInfoDriverStat;
import com.jw.server.model.BaseInfoPassenger;
import com.jw.server.model.BaseInfoVehicle;
import com.jw.server.model.BaseInfoVehicleInsurance;
import com.jw.server.model.BaseInfoVehicleTotalMile;
import com.jw.server.service.BaseInfoCompanyFareRepository;
import com.jw.server.service.BaseInfoCompanyPayRepository;
import com.jw.server.service.BaseInfoCompanyPermitRepository;
import com.jw.server.service.BaseInfoCompanyRepository;
import com.jw.server.service.BaseInfoCompanyServiceRepository;
import com.jw.server.service.BaseInfoCompanyStatRepository;
import com.jw.server.service.BaseInfoDriverAppRepository;
import com.jw.server.service.BaseInfoDriverEducateRepository;
import com.jw.server.service.BaseInfoDriverRepository;
import com.jw.server.service.BaseInfoDriverStatRepository;
import com.jw.server.service.BaseInfoPassengerRepository;
import com.jw.server.service.BaseInfoVehicleInsuranceRepository;
import com.jw.server.service.BaseInfoVehicleRepository;
import com.jw.server.service.BaseInfoVehicleTotalMileRepository;
import com.jw.server.util.ByteUtil;

/**
 * 解析所有基本信息数据入库
 * 
 * @author xfm 2018年5月13日
 */

@RestController
@RequestMapping("/baseinfo")
public class AcceptDataController {

	@Autowired
	BaseInfoCompanyRepository baseInfoCompanyRepository;

	@Autowired
	BaseInfoCompanyStatRepository baseInfoCompanyStatRepository;

	@Autowired
	BaseInfoCompanyPayRepository baseInfoCompanyPayRepository;

	@Autowired
	BaseInfoCompanyServiceRepository baseInfoCompanyServiceRepository;

	@Autowired
	BaseInfoCompanyPermitRepository baseInfoCompanyPermitRepository;

	@Autowired
	BaseInfoCompanyFareRepository baseInfoCompanyFareRepository;

	@Autowired
	BaseInfoDriverRepository baseInfoDriverRepository;

	@Autowired
	BaseInfoDriverAppRepository baseInfoDriverAppRepository;

	@Autowired
	BaseInfoDriverEducateRepository baseInfoDriverEducateRepository;

	@Autowired
	BaseInfoDriverStatRepository baseInfoDriverStatRepository;

	@Autowired
	BaseInfoPassengerRepository baseInfoPassengerRepository;

	@Autowired
	BaseInfoVehicleRepository baseInfoVehicleRepository;

	@Autowired
	BaseInfoVehicleInsuranceRepository baseInfoVehicleInsuranceRepository;

	@Autowired
	BaseInfoVehicleTotalMileRepository baseInfoVehicleTotalMileRepository;

	@PostMapping("/companypay")
	public String companypay(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoCompanyPay> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoCompanyPay> li = o
						.getBaseInfoCompanyPayList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoCompanyPay> l = li.iterator();
					basepay = new ArrayList<BaseInfoCompanyPay>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoCompanyPay base = l.next();
						basepay.add(BaseInfoCompanyPay.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoCompanyPay表的数据条数："+basepay.size());
			baseInfoCompanyPayRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/company")
	public String company(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoCompany> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoCompany> li = o.getBaseInfoCompanyList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoCompany> l = li.iterator();
					basepay = new ArrayList<BaseInfoCompany>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoCompany base = l.next();
						basepay.add(BaseInfoCompany.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoCompany表的数据条数："+basepay.size());
			baseInfoCompanyRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/companyfare")
	public String companyFare(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoCompanyFare> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoCompanyFare> li = o
						.getBaseInfoCompanyFareList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoCompanyFare> l = li.iterator();
					basepay = new ArrayList<BaseInfoCompanyFare>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoCompanyFare base = l.next();
						basepay.add(BaseInfoCompanyFare.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoCompanyFare表的数据条数："+basepay.size());
			baseInfoCompanyFareRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/companypermit")
	public String companyPermit(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoCompanyPermit> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoCompanyPermit> li = o
						.getBaseInfoCompanyPermitList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoCompanyPermit> l = li.iterator();
					basepay = new ArrayList<BaseInfoCompanyPermit>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoCompanyPermit base = l.next();
						basepay.add(BaseInfoCompanyPermit.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoCompanyPermit表的数据条数："+basepay.size());
			baseInfoCompanyPermitRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	/*
	 * @PostMapping("/companypermit") public String companyPermit(@RequestBody
	 * byte[] content) { byte[] b = null; //解压数据 b =
	 * ByteUtil.decompress(content); try { //拿到第一层 OTIpcList b1 =
	 * OTIpcDef.OTIpcList.parseFrom(b); ListIterator<OTIpcDef.OTIpc> iter =
	 * b1.getOtpicList() .listIterator(); List<BaseInfoCompanyPermit>
	 * basepay=null; while (iter.hasNext()) { OTIpcDef.OTIpc o = iter.next();
	 * List<OTIpcDef.BaseInfoCompanyPermit> li = o
	 * .getBaseInfoCompanyPermitList(); if (li != null && li.size() > 0) {
	 * Iterator<OTIpcDef.BaseInfoCompanyPermit> l = li.iterator(); basepay=new
	 * ArrayList<BaseInfoCompanyPermit>(); while (l.hasNext()) {
	 * OTIpcDef.BaseInfoCompanyPermit base = l.next();
	 * basepay.add(BaseInfoCompanyPermit.convertPojo(base)); } } } //将数据存入数据库
	 * basepay System.out.println(basepay.size());
	 * baseInfoCompanyPermitRepository.save(basepay);
	 * 
	 * } catch (Exception e) { e.printStackTrace(); } return ""; }
	 */

	@PostMapping("/companyservice")
	public String companyService(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoCompanyService> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoCompanyService> li = o
						.getBaseInfoCompanyServiceList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoCompanyService> l = li.iterator();
					basepay = new ArrayList<BaseInfoCompanyService>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoCompanyService base = l.next();
						basepay.add(BaseInfoCompanyService.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoCompanyService表的数据条数："+basepay.size());
			baseInfoCompanyServiceRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/companystat")
	public String companyStat(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoCompanyStat> stat = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoCompanyStat> li = o
						.getBaseInfoCompanyStatList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoCompanyStat> l = li.iterator();
					stat = new ArrayList<BaseInfoCompanyStat>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoCompanyStat base = l.next();
						stat.add(BaseInfoCompanyStat.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoCompanystat表的数据条数："+stat.size());
			baseInfoCompanyStatRepository.save(stat);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/driver")
	public String baseinfoDriver(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoDriver> stat = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoDriver> li = o.getBaseInfoDriverList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoDriver> l = li.iterator();
					stat = new ArrayList<BaseInfoDriver>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoDriver base = l.next();
						stat.add(BaseInfoDriver.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoDriver表的数据条数："+stat.size());
			baseInfoDriverRepository.save(stat);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/driverapp")
	public String baseinfoDriverApp(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoDriverApp> stat = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoDriverApp> li = o
						.getBaseInfoDriverAppList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoDriverApp> l = li.iterator();
					stat = new ArrayList<BaseInfoDriverApp>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoDriverApp base = l.next();
						stat.add(BaseInfoDriverApp.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoDriverapp表的数据条数："+stat.size());
			baseInfoDriverAppRepository.save(stat);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/drivereducate")
	public String baseinfoDriverEducate(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoDriverEducate> stat = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoDriverEducate> li = o
						.getBaseInfoDriverEducateList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoDriverEducate> l = li.iterator();
					stat = new ArrayList<BaseInfoDriverEducate>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoDriverEducate base = l.next();
						stat.add(BaseInfoDriverEducate.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoDriverEducate表的数据条数："+stat.size());
			baseInfoDriverEducateRepository.save(stat);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/driverstat")
	public String baseinfoDriverStat(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoDriverStat> stat = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoDriverStat> li = o
						.getBaseInfoDriverStatList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoDriverStat> l = li.iterator();
					stat = new ArrayList<BaseInfoDriverStat>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoDriverStat base = l.next();
						stat.add(BaseInfoDriverStat.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoDriverStat表的数据条数："+stat.size());
			baseInfoDriverStatRepository.save(stat);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/passenger")
	public String baseinfoPassenger(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoPassenger> stat = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoPassenger> li = o
						.getBaseInfoPassengerList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoPassenger> l = li.iterator();
					stat = new ArrayList<BaseInfoPassenger>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoPassenger base = l.next();
						stat.add(BaseInfoPassenger.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoPassenger表的数据条数："+stat.size());
			baseInfoPassengerRepository.save(stat);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/vehicle")
	public String baseinfoVehicle(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoVehicle> stat = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoVehicle> li = o.getBaseInfoVehicleList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoVehicle> l = li.iterator();
					stat = new ArrayList<BaseInfoVehicle>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoVehicle base = l.next();
						stat.add(BaseInfoVehicle.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoVehicle表的数据条数："+stat.size());
			baseInfoVehicleRepository.save(stat);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/vehicleinsurance")
	public String vehicleinsurance(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoVehicleInsurance> stat = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoVehicleInsurance> li = o
						.getBaseInfoVehicleInsuranceList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoVehicleInsurance> l = li
							.iterator();
					stat = new ArrayList<BaseInfoVehicleInsurance>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoVehicleInsurance base = l.next();
						stat.add(BaseInfoVehicleInsurance.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收到baseInfoVehicleInsurance表的数据条数："+stat.size());
			baseInfoVehicleInsuranceRepository.save(stat);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/vehicletotalmile")
	public String vehicletotalmile(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<BaseInfoVehicleTotalMile> stat = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.BaseInfoVehicleTotalMile> li = o
						.getBaseInfoVehicleTotalMileList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.BaseInfoVehicleTotalMile> l = li
							.iterator();
					stat = new ArrayList<BaseInfoVehicleTotalMile>();
					while (l.hasNext()) {
						OTIpcDef.BaseInfoVehicleTotalMile base = l.next();
						stat.add(BaseInfoVehicleTotalMile.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收baseInfoVehicleTotalMile表的数据条数："+stat.size());
			baseInfoVehicleTotalMileRepository.save(stat);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

}
